#----------------------------------------------------------
# First Processor
#----------------------------------------------------------
	org		0x0000							# first processor p0
	ori		$sp, $zero, 0x3ffc					# stack
	jal		mainp0
#	jal		MERGE							# go to program
	halt


# pass in an address to lock function in argument register 0
# returns when lock is available
lock:
aquire:
	ll              $t1, 0($15)       					# load lock location
        bne             $t1, $0, aquire   					# wait on lock to be open
        addiu            $t1, $t1, 1
        sc              $t1, 0($15)
        beq             $t1, $0, lock     					# if sc failed retry
        jr              $ra

	
# pass in an address to unlock function in argument register 0
# returns when lock is free
unlock: 
	sw              $0, 0($15)
        jr              $ra							# back to caller


# main function does something ugly but demonstrates beautifully
mainp0:
	push	$ra								# save return address

	ori	$15, $zero, l0    #a0-a3 are 4-7    t0-t7 are 8-15
	jal	lock

	ori $17, $zero, 0x1		#don't touch 17

	
	ori 	$1, $zero, 0xC8			#length
	SRL	$1, $1, 1

	ori	$2, $zero, sortdata

	subu	$3, $1, $17		#outer loop variable

outerloop:
	beq 	$3, $zero, endouter		#if ($3 > 0)

	ori 	$4, $zero, 0x0		#j = sortdata
	ori	$8, $zero, sortdata
innerloop:
	SLTU 	$5, $4, $3
	BEQ 	$5, $zero, endinner		#if ($4 < $3)

	LW	$6, 0($8)
	LW	$7, 4($8)
if:	#$6 > $7
	SLTU	$5, $7, $6
	BEQ	$5, $zero, noswap
	SW	$7, 0($8)
	SW	$6, 4($8)

noswap:
	ADDIU 	$4, $4, 0x1		#inc inner loop variable
	ADDIU	$8, $8, 0x4
	BEQ	$zero, $zero, innerloop
endinner:

	SUBU 	$3, $3, $17  			#dec outer loop variable
	BEQ	$zero, $zero, outerloop
endouter:

	ori	$15, $zero, l0
	jal	unlock

	pop		$ra			# get return address
	jr		$ra			# return to caller



MERGE:	push 	$ra	
	ori  	$15,$zero,l1
	jal	lock
	ori	$17,$zero,1

	ori     $7,$zero,0x1000

 	ori	$8,$zero,0x1000	
	addiu	$8,$8,400   #middle of the data

	ori	$14, $zero, result    # Result space
	ori	$13, $zero, 100      # length of right
	ori	$12, $zero, 100      # lenght of left 

SLOOP1:
	lw	$3, 0($7)		# Load left into $3
		
	lw	$4, 0($8)		# Load Right into $4
	
	BEQ	$12,$zero,ONLYR          # if the left is empty go to right 
	BEQ	$13,$zero,ONLYL        #else if the right is done go to left
	
	slt     $5,$3,$4
	BEQ	$5,$17,Dswap             # if left is less than right then Dswap(store left)  else FALL through and store right 
ONLYR:  BEQ	$13,$zero,ENTDone
	sw	$4,0($14)
	addiu	$8,$8,4                     # incriment right address
	subu	$13,$13,$17                 # decriment right length
	
	BEQ	$zero,$zero,SwapDone        # branch all to swapdone and re loop	
ONLYL:  BEQ	$12,$zero,ENTDone
Dswap:	sw	$3,0($14)	           
	addiu	$7,$7,4			# incriment left address
	subu	$12,$12,$17		# decriment left length
SwapDone:  
	addiu	$14,$14,4
	BEQ	$zero,$zero,SLOOP1	
ENTDone:
	ori	$15, $zero, l1
	jal 	unlock
	pop	$ra
	jr	$ra


l0:
	cfw	0x0
l1:
	cfw	0x0


#----------------------------------------------------------
# Second Processor
#----------------------------------------------------------
	org		0x200							# second processor p1
	ori		$sp, $zero, 0x7ffc	# stack
	jal		mainp1							# go to program
	JAL		MERGE
	halt

# main function does something ugly but demonstrates beautifully
mainp1:
	push	$ra								# save return address
	ori	$15, $zero, l1    #a0-a3 are 4-7    t0-t7 are 8-15
	jal	lock

	ori $17, $zero, 0x1		#don't touch 17

	ori 	$1, $zero, 0xC8			#length
	SLL	$1, $1, 1			#length * 2
	ori	$2, $zero, sortdata
	ADDU	$2, $2, $1		# where we start sorting ie the second half

	SRL	$1, $1, 2			#length/2  (amount we're sorting)

	subu	$3, $1, $17		#outer loop variable

outerloop1:
	beq 	$3, $zero, endouter1		#if ($3 > 0)

	ori 	$4, $zero, 0x0		#j = sortdata
	or	$8, $zero, $2
innerloop1:
	SLTU 	$5, $4, $3
	BEQ 	$5, $zero, endinner1		#if ($4 < $3)

	LW	$6, 0($8)
	LW	$7, 4($8)
if1:	#$6 > $7
	SLTU	$5, $7, $6
	BEQ	$5, $zero, noswap1
	SW	$7, 0($8)
	SW	$6, 4($8)

noswap1:
	ADDIU 	$4, $4, 0x1		#inc inner loop variable
	ADDIU	$8, $8, 0x4
	BEQ	$zero, $zero, innerloop1
endinner1:

	SUBU 	$3, $3, $17  			#dec outer loop variable
	BEQ	$zero, $zero, outerloop1
endouter1:
	ori 	$15,$zero,l1
	jal 	unlock
	pop		$ra							# get return address
	jr		$ra							# return to caller

	org 0x1000
sortdata: 
cfw 0x087d
cfw 0x5fcb
cfw 0xa41a
cfw 0x4109
cfw 0x4522
cfw 0x700f
cfw 0x766d
cfw 0x6f60
cfw 0x8a5e
cfw 0x9580
cfw 0x70a3
cfw 0xaea9
cfw 0x711a
cfw 0x6f81
cfw 0x8f9a
cfw 0x2584
cfw 0xa599
cfw 0x4015
cfw 0xce81
cfw 0xf55b
cfw 0x399e
cfw 0xa23f
cfw 0x3588
cfw 0x33ac
cfw 0xbce7
cfw 0x2a6b
cfw 0x9fa1
cfw 0xc94b
cfw 0xc65b
cfw 0x0068
cfw 0xf499
cfw 0x5f71
cfw 0xd06f
cfw 0x14df
cfw 0x1165
cfw 0xf88d
cfw 0x4ba4
cfw 0x2e74
cfw 0x5c6f
cfw 0xd11e
cfw 0x9222
cfw 0xacdb
cfw 0x1038
cfw 0xab17
cfw 0xf7ce
cfw 0x8a9e
cfw 0x9aa3
cfw 0xb495
cfw 0x8a5e
cfw 0xd859
cfw 0x0bac
cfw 0xd0db
cfw 0x3552
cfw 0xa6b0
cfw 0x727f
cfw 0x28e4
cfw 0xe5cf
cfw 0x163c
cfw 0x3411
cfw 0x8f07
cfw 0xfab7
cfw 0x0f34
cfw 0xdabf
cfw 0x6f6f
cfw 0xc598
cfw 0xf496
cfw 0x9a9a
cfw 0xbd6a
cfw 0x2136
cfw 0x810a
cfw 0xca55
cfw 0x8bce
cfw 0x2ac4
cfw 0xddce
cfw 0xdd06
cfw 0xc4fc
cfw 0xfb2f
cfw 0xee5f
cfw 0xfd30
cfw 0xc540
cfw 0xd5f1
cfw 0xbdad
cfw 0x45c3
cfw 0x708a
cfw 0xa359
cfw 0xf40d
cfw 0xba06
cfw 0xbace
cfw 0xb447
cfw 0x3f48
cfw 0x899e
cfw 0x8084
cfw 0xbdb9
cfw 0xa05a
cfw 0xe225
cfw 0xfb0c
cfw 0xb2b2
cfw 0xa4db
cfw 0x8bf9
cfw 0x12f7
cfw 0x087d
cfw 0x5fcb
cfw 0x24b2
cfw 0x4b09
cfw 0x4522
cfw 0x700f
cfw 0x766d
cfw 0x6f60
cfw 0x8255
cfw 0x9580
cfw 0x7023
cfw 0x2529
cfw 0x7bb2
cfw 0x6f8b
cfw 0x8f92
cfw 0x2584
cfw 0x2599
cfw 0x40b5
cfw 0xc58b
cfw 0xf55b
cfw 0x3995
cfw 0x223f
cfw 0x3588
cfw 0x332c
cfw 0xbc57
cfw 0x226b
cfw 0x9f2b
cfw 0xc94b
cfw 0xc65b
cfw 0x0068
cfw 0xf499
cfw 0x5f7b
cfw 0xd06f
cfw 0xb4df
cfw 0xbb65
cfw 0xf88d
cfw 0x4b24
cfw 0x2574
cfw 0x5c6f
cfw 0xdbb5
cfw 0x9222
cfw 0x2cdb
cfw 0xb038
cfw 0x2bb7
cfw 0xf7c5
cfw 0x8295
cfw 0x9223
cfw 0xb495
cfw 0x8255
cfw 0xd859
cfw 0x0b2c
cfw 0xd0db
cfw 0x3552
cfw 0x26b0
cfw 0x727f
cfw 0x2854
cfw 0x55cf
cfw 0xb63c
cfw 0x34bb
cfw 0x8f07
cfw 0xf2b7
cfw 0x0f34
cfw 0xd2bf
cfw 0x6f6f
cfw 0xc598
cfw 0xf496
cfw 0x9292
cfw 0xbd62
cfw 0x2b36
cfw 0x8b02
cfw 0xc255
cfw 0x8bc5
cfw 0x22c4
cfw 0xddc5
cfw 0xdd06
cfw 0xc4fc
cfw 0xfb2f
cfw 0x555f
cfw 0xfd30
cfw 0xc540
cfw 0xd5fb
cfw 0xbd2d
cfw 0x45c3
cfw 0x7082
cfw 0x2359
cfw 0xf40d
cfw 0xb206
cfw 0xb2c5
cfw 0xb447
cfw 0x3f48
cfw 0x8995
cfw 0x8084
cfw 0xbdb9
cfw 0x2052
cfw 0x5225
cfw 0xfb0c
cfw 0xb2b2
cfw 0x24db
cfw 0x8bf9
cfw 0xb2f7

	org 0x3000
result:
	cfw 0x0
